package transaction.lcn.account.mapper;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;
import tk.mybatis.springboot.util.MyMapper;
import transaction.lcn.account.model.AccountTbl;
import transaction.lcn.common.tcc.WalletPayRequestVO;

@Repository
public interface AccountTblMapper extends MyMapper<AccountTbl> {

    @Update("UPDATE account_tbl SET money = money-#{num} WHERE id=#{id} AND money-#{num}>=0")
    int updateByPrimaryKeySelectiveNew(@Param("id") Integer id, @Param("num") Integer num);

    @Update("update `wallet` set freeze_amount = freeze_amount - #{payAmount}, total_amount = total_amount - #{payAmount} where user_id = #{userId}")
    int doConfirmPay(WalletPayRequestVO param);

    @Update("update `wallet` set freeze_amount = freeze_amount + #{payAmount} where user_id = #{userId} and (total_amount - freeze_amount) >= #{payAmount}")
    int doTryPay(WalletPayRequestVO param);

    @Update("update `wallet` set freeze_amount = freeze_amount - #{payAmount} where user_id = #{userId}")
    int doCancelPay(WalletPayRequestVO param);
}